/*
 *  To change this template, choose Tools | Templates
 *  and open the template in the editor.
 */
package projetomsn.Client.DataBase;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Locale;
import java.util.ResourceBundle;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author Administrator
 */
public class FriendManager {

    static final String DB_DRIVER = "com.mysql.jdbc.Driver";
    static final String DB_URL = "jdbc:mysql://localhost:3306/humberto";
    static final String DB_USER = "root";
    String DB_PASSWORD = "0379981";
    private Connection connection;
    private Statement statement;
    private ResultSet resultSet;
    private static Logger log = Logger.getLogger(FriendManager.class.getName());
    //String language;
    //private ResourceBundle myResources;
    private ResourceBundle myResources =
            ResourceBundle.getBundle("Properties.messages", Locale.getDefault());

    public FriendManager() {
        /*Locale.getDefault();

        if(Locale.getDefault().toString().equalsIgnoreCase("pt_BR"))
        language = "Properties/messages_pt_BR";
        else if(Locale.getDefault().toString().equalsIgnoreCase("en_US"))
        language = "Properties/messages_en_US";

        myResources =
        ResourceBundle.getBundle(language , Locale.getDefault());*/
    }

    public boolean conecta() {
        boolean result = true;
        try {
            Class.forName(DB_DRIVER);
            connection = (Connection) DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD);
            

        } catch (ClassNotFoundException Driver) {
            result = false;
           
        } catch (SQLException Fonte) {
            result = false;
           

        }
        return result;

    }

    public void desconecta() {
        boolean result = true;
        try {
            connection.close();

        } catch (SQLException erroSQL) {
            result = false;
        }
    }

    public void executeSQL(String sql) {
        try {
            statement = (Statement) connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
                    ResultSet.CONCUR_READ_ONLY);
            resultSet = statement.executeQuery(sql);
        } catch (SQLException sqlex) {
            sqlex.printStackTrace();
        }
    }

    public int getUserName(String email) throws SQLException {
        // our SQL SELECT query. 
        // if you only need a few columns, specify them by name instead of using "*"
        int id = -1;

        String query = "SELECT * FROM users WHERE email='" + email + "'";

        // create the java statement
        Statement st = (Statement) connection.createStatement();

        // execute the query, and get a java resultSet
        ResultSet rs = st.executeQuery(query);

        // iterate through the java resultSet
        if (rs.next()) {

            String firstName = rs.getString("email");
            String lastName = rs.getString("name");
            System.out.println(firstName + " " + lastName);
            return id;
        }
        st.close();
        return id;
    }

    public void addNewUser(String email, String name) throws SQLException {
        String query = "INSERT INTO humberto.users VALUE ('" + email + "','" + name + "')";

        Statement st = (Statement) connection.createStatement();

        // execute the query, and get a java resultSet
        int rs = st.executeUpdate(query);


    }

    public ArrayList<String> getFriends() throws SQLException {

        ArrayList<String> friendList = new ArrayList<String>();

        String query = "SELECT * FROM users ";

        // create the java statement
        Statement st = (Statement) connection.createStatement();

        // execute the query, and get a java resultSet
        ResultSet rs = st.executeQuery(query);

        // iterate through the java resultSet
        while (rs.next()) {
            friendList.add(rs.getString("email"));
            friendList.add(rs.getString("name"));

        }

        st.close();

        return friendList;
    }
}
